package com.yo.yorpc.proxy;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.yo.yorpc.RPCApplication;
import com.yo.yorpc.model.RpcRequest;
import com.yo.yorpc.model.RpcResponse;
import com.yo.yorpc.serializer.JdkSerializer;
import com.yo.yorpc.serializer.Serializer;
import com.yo.yorpc.serializer.SerializerFactory;

import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.http.HttpClient;

/**
 * 动态代理
 */
public class ServiceProxy implements InvocationHandler {
    /**
     *调用代理
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //指定序列化器
        final  Serializer serializer= SerializerFactory.getInstance(RPCApplication.getRpcConfig().getSerializer());

        //构造请求
        RpcRequest rpcRequest=RpcRequest.builder()
                .serviceName(method.getDeclaringClass().getName())
                .methodName(method.getName())
                .parameterTypes(method.getParameterTypes())
                .args(args)
                .build();

        try {
            //序列化
            byte[] bodyBytes=serializer.serialize(rpcRequest);
            //发送请求
            //todo 注意这里地址被硬编码了（需要使用注册中心和服务发现机制解决）
            try (HttpResponse httpResponse= HttpRequest.post("http://localhost:8080")
                    .body(bodyBytes)
                    .execute()){
                byte[]result=httpResponse.bodyBytes();
                //反序列化
                RpcResponse rpcResponse=serializer.deserialize(result,RpcResponse.class);
                return rpcResponse.getData();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    return null;
    }

}
